<template>
  <div class="content">
    <h1 class="title"><i class="fas fa-bed"></i> 客房状态</h1>

    <div class="filters">
      <select v-model="filterStatus" class="select-field">
        <option value="all">所有状态</option>
        <option value="available">可入住</option>
        <option value="occupied">已入住</option>
      </select>
      <select v-model="filterFloor" class="select-field">
        <option value="all">所有楼层</option>
        <option value="1">一楼</option>
        <option value="2">二楼</option>
        <option value="3">三楼</option>
      </select>
    </div>

    <div class="floor" v-for="floor in [1, 2, 3]" :key="floor">
      <h2 class="floor-title"><i class="fas fa-level-up-alt"></i> {{ floor }}楼</h2>
      <div class="room-grid">
        <div
          v-for="room in filteredRooms.filter(r => r.number.startsWith(floor))"
          :key="room.number"
          class="room-card"
          :class="room.status"
          @mouseenter="showBookingInfo(room)"
          @mouseleave="hideBookingInfo"
        >
          <h3>{{ room.number }}</h3>
          <p>{{ room.type }} ¥{{ room.price }}</p>
          <p>{{ room.status === 'available' ? '可入住' : '已入住' }}</p>

          <!-- 预订信息提示框 -->
          <div v-if="showTooltip && currentRoom && currentRoom.number === room.number" class="booking-tooltip">
            <div v-if="currentBooking">
              <h4>预订信息</h4>
              <p><strong>客人:</strong> {{ currentBooking.guestName }}</p>
              <p><strong>电话:</strong> {{ currentBooking.guestPhone }}</p>
              <p><strong>入住:</strong> {{ formatDate(currentBooking.checkInDate) }}</p>
              <p><strong>退房:</strong> {{ formatDate(currentBooking.checkOutDate) }}</p>

              <div v-if="currentBookingServices.length > 0">
                <h5>已添加服务:</h5>
                <ul>
                  <li v-for="(service, index) in currentBookingServices" :key="index">
                    {{ service.name }} - ¥{{ service.price }}
                  </li>
                </ul>
              </div>
              <p v-else>暂无服务</p>
            </div>
            <div v-else>
              无预订信息
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'RoomStatus',
  data() {
    return {
      filterStatus: 'all',
      filterFloor: 'all',
      showTooltip: false,
      currentRoom: null,
      currentBooking: null,
      currentBookingServices: []
    }
  },
  computed: {
    rooms() {
      return this.$store.state.rooms
    },
    bookings() {
      return this.$store.state.bookings
    },
    incomeRecords() {
      return this.$store.state.incomeRecords
    },
    filteredRooms() {
      let filtered = this.rooms

      if (this.filterStatus!== 'all') {
        filtered = filtered.filter(room => room.status === this.filterStatus)
      }

      if (this.filterFloor!== 'all') {
        filtered = filtered.filter(room => room.number.startsWith(this.filterFloor))
      }

      return filtered
    }
  },
  methods: {
    toggleRoomStatus(room) {
      const newStatus = room.status === 'available'? 'occupied' : 'available'
      this.$store.dispatch('updateRoomStatus', {
        roomNumber: room.number,
        status: newStatus
      })
    },

    showBookingInfo(room) {
      if (room.status === 'occupied') {
        this.currentRoom = room
        this.currentBooking = this.bookings.find(b => b.roomNumber === room.number)

        if (this.currentBooking) {
          // 从收入记录中获取服务信息
          this.currentBookingServices = this.incomeRecords
            .filter(record =>
              record.note.includes(`房间 ${room.number}`) &&
              record.type!== '房费'
            )
            .map(record => ({
              name: record.type,
              price: record.amount
            }))
        }

        this.showTooltip = true
      }
    },

    hideBookingInfo() {
      this.showTooltip = false
      this.currentRoom = null
      this.currentBooking = null
      this.currentBookingServices = []
    },

    formatDate(dateStr) {
      if (!dateStr) return '-'
      const date = new Date(dateStr)
      return date.toLocaleDateString()
    }
  }
}
</script>

<style scoped>
.content {
  margin-left: 300px;
  padding: 40px;
  background-color: #f4f6f9; /* 更高级的背景颜色 */
}

.title {
  font-size: 32px;
  color: #333;
  margin-bottom: 20px;
}

.filters {
  margin-bottom: 20px;
  display: flex;
}

.select-field {
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  margin-right: 15px;
  font-size: 16px;
  width: 150px;
}

.floor {
  margin-bottom: 30px;
}

.floor-title {
  font-size: 24px;
  color: #333;
  border-bottom: 1px solid #ddd;
  padding-bottom: 5px;
  margin-bottom: 15px;
}

.room-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 20px;
}

.room-card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: relative;
}

.room-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.room-card h3 {
  margin-top: 0;
  font-size: 20px;
  color: #333;
}

.room-card p {
  margin: 5px 0;
  color: #666;
}

.room-card.available {
  background-color: #e8f5e9;
  border-color: #a5d6a7;
}

.room-card.occupied {
  background-color: #ffebee;
  border-color: #ef9a9a;
}

.booking-tooltip {
  position: absolute;
  z-index: 100;
  width: 300px;
  padding: 15px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(100% + 10px);
  text-align: left;
}

.booking-tooltip h4 {
  margin-top: 0;
  color: #333;
  border-bottom: 1px solid #eee;
  padding-bottom: 8px;
}

.booking-tooltip h5 {
  margin-bottom: 5px;
  color: #555;
}

.booking-tooltip ul {
  padding-left: 20px;
  margin: 5px 0;
}

.booking-tooltip li {
  margin-bottom: 3px;
}
</style>